<?php

namespace Home\Controller;
use Think\Upload\Driver\Qiniu\QiniuStorage;
use Think\AjaxPage;
use Think\Page;
class BimController extends BaseController {
    /**
     * 政府批复文件列表
     */
    public function basics(){
        $Modal =M('file_cat');
        $p = I('p',1);
        $pid = session('pid');
        if(!$pid){
            $pid = 0;
        }
        $res = $Modal->where('pid='.$pid)->order('id desc')->page($p.',10')->select();
        foreach ($res as $key=>$val){
            if($val['type'] !=0){
                $type[] =$val['type'];
            }
        }
        if(!in_array(1,$type) && is_numeric($pid) && $pid !=0){
            $data['name'] = '图纸';
            $data['pid'] = $pid;
            $data['add_time'] = time();
            $data['type'] =1;
            $Modal->add($data);
        }
        if(!in_array(2,$type) && is_numeric($pid) && $pid !=0){
            $data['name'] = '设计变更';
            $data['pid'] = $pid;
            $data['add_time'] = time();
            $data['type'] =2;
            $Modal->add($data);
        }
        if(!in_array(3,$type) && is_numeric($pid) && $pid !=0){
            $data['name'] = '形象进度';
            $data['pid'] = $pid;
            $data['add_time'] = time();
            $data['type'] =3;
            $Modal->add($data);
        }
        if(!in_array(4,$type) && is_numeric($pid) && $pid !=0){
            $data['name'] = '任务附件';
            $data['pid'] = $pid;
            $data['add_time'] = time();
            $data['type'] =4;
            $Modal->add($data);
        }
        $where['pid'] = array('eq',$pid);
        $where['name'] = array('neq','任务附件');
        $res = $Modal->where($where)->order('id desc')->page($p.',10')->select();
        $count = $Modal->where($where)->count();
        $Page = new \Think\Page($count,10);
        $show = $Page->show();
        $this->assign('file',$res);
        $this->assign('page',$show);
        $this->display();
    }
    
    /**
     * 政府批复文件列表
     */
    public function gover(){
        $cid = I('get.fid');
        $data = M('file_cat')->where('id='.$cid)->find();
        $type = $data['type'];
        $fname = $data['name'];
        $this->assign('cid',$cid);
        $this->assign('type',$type);
        $this->assign('fname',$fname);
        $this->display();
    }
    
    /**
     * ajax文件列表
     */
    public function ajaxgover(){
        $where = array();
        $search = I('post.name');
        $type = I('post.type');
        $cid = I('post.cid');
        $pid = session('pid');
        if(is_numeric($cid)){//
            $where[] = "file_cid='".addslashes($cid)."'";
        }
        if($search){
            $where[] = "name like '%$search%' ";
        }
         $where[] = "pid='".addslashes($pid)."'";
        $where = implode(' AND ', $where);
        $model = M('file_download');
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        //  搜索条件下 分页赋值
        foreach($where as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }
        $List = $model->where($where)->order("add_time desc")->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();
        
        if(!is_numeric($type)){
            $type = 0;
        }
        if($type==3){
            $image = M('project')->field('image')->where('id='.$pid)->find();
            $this->assign('image',$image['image']);
        }
        $this->assign('cid',$cid);
        $this->assign('data',$List);
        $this->assign('type',$type);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }
    
    
    /**
     * 上传文件
     */
    public function upload(){
          $tid = I('post.tid',0);
          $setting = C('UPLOAD_FILE_QINIU');
          $Upload = new \Think\Upload($setting);
          $info = $Upload->upload($_FILES);
          $path = str_replace('/','_',$info['file']['savepath']);
          $filename = $path.$info['file']['savename'];//七牛云保存的文件名称
          if(!$info)
          {
             $data = ['msg'=>'上传失败，'.$Upload->getError()];
           //  echo "<script>alert('{$data}')</script>";
           $this->error('上传失败'.$Upload->getError());
          }
          else
          {
               $tid = I('post.tid',0);
               $name = session('admin');
               $uname = $name['name'];
               $add['file_path'] = $info['file']['url'];
               $add['name'] = $_FILES['file']['name'];
               $add['upload'] = $uname;
               $add['mname'] = $filename;
               $add['pid'] = session('pid');
               $add['add_time'] = time();
               $add['ext'] = $info['file']['ext'];
               $add['size'] = round($_FILES['file']['size']/1024/1024,2);
               $add['file_cid'] = I('post.key');
               $add['tid'] = $tid;
               M('file_download')->add($add);
               adminLog("上传文件".$_FILES['file']['name']."成功",__ACTION__);
               $pid = session('pid');
               if($pid && $tid == 0){
                   $jo = M('setpm')->field('join')->where('product_id='.$pid)->find();
                   $join = explode(',',$jo['join']);
                   SendMessage($join,'文件上传',$uname.'上传了'.$_FILES['file']['name'].'文件');
               }
         }
 
         echo json_encode($data);
         exit;
    }
    
    /**
     * 删除文件
     */
    public function delFile()
    {
        $file_name = I('post.filename');
        $id = I('post.id');
        if(empty($file_name)){
            exit(json_encode(array('errNo'=>1,'msg'=>'文件出现未知错误,请重试')));
        }
        $setting = C('UPLOAD_FILE_QINIU');
        $Qiniu = new QiniuStorage($setting['driverConfig']);
        $result = $Qiniu->del($file_name);
        $error = $Qiniu->errorStr;//错误信息
        if(is_array($result) && !($error))
        {
            M('file_download')->where(array('id'=>$id))->delete();
            adminLog("删除文件".$file_name."成功",__ACTION__);
            exit(json_encode(array('errNo'=>0,'msg'=>'删除文件成功')));
        }
        else
        {
            exit(json_encode(array('errNo'=>1,'msg'=>'删除文件失败'.$error)));
        }
    }
    
    /**
     * 文件下载
     */
    public function downLink()
    {
       // $file_name = '2017-02-22_58ad455a458801.pdf';
        $file_name = I('get.filename');
        $name = I('get.name');
        $id = I('get.id');
        $setting = C('UPLOAD_FILE_QINIU');
        $Qiniu = new QiniuStorage($setting['driverConfig']);
        $url = $Qiniu->downLink($file_name);
        $file = $Qiniu->privateDownloadUrl($url);
       // header("Location: $file");
       // header("Content-type:application/zip");
        // 文件将被称为 downloaded.pdf
        if($file){
            header("Content-Type: application/force-download");//强制下载
            header("Content-Disposition:attachment;filename=".$name);
            readfile($file);
            M('file_download')->where('id='.$id)->setInc('down_num');
            adminLog("下载了".$name."文件",__ACTION__);
        }
    }
    /**
     * 新建文件夹
     */
    public function add_file()
    {
        $data = I('post.');
        $Modal = M('file_cat');
        if($data['id'] !=0){
           $res =  M('file_download')->where('file_cid='.$data['id'])->count();
           if($res){
               exit(json_encode(array('errNo'=>1,'msg'=>'请先删除子文件')));
           }else{
               $r = $Modal->where('id='.$data['id'])->delete();
               if($r){
                   adminLog("删除了".$data['id']."文件夹",__ACTION__);
                   exit(json_encode(array('errNo'=>0,'msg'=>'已删除')));
               }else{
                   exit(json_encode(array('errNo'=>1,'msg'=>'删除失败')));
               }
           }
        }else{
            if($data['name']==''){
                exit(json_encode(array('errNo'=>1,'msg'=>'文件夹名不能为空')));
            }
            $data['add_time'] = time();
            $data['pid'] = session('pid');
            $res = $Modal->add($data);
            if($res){
                adminLog("增加了".$data['id']."文件夹",__ACTION__);
                exit(json_encode(array('errNo'=>0,'msg'=>'新建成功')));
            }else{
                exit(json_encode(array('errNo'=>1,'msg'=>'新建失败,请重试')));
            }
        }
       
    }
    
    /**
     * editfile
     */
    function editfile(){
        $id = I('post.id');
        $name = I('post.name');
        if(!id){
            exit(json_encode(array('errNo'=>1,'msg'=>'id有误，请重试')));
        }else{
           $r = M('file_cat')->where('id='.$id)->save(array('name'=>$name));
           if($r){
               adminLog("修改了".$id."文件夹的名称",__ACTION__);
               exit(json_encode(array('errNo'=>0,'msg'=>'修改成功')));
           }else{
               exit(json_encode(array('errNo'=>1,'msg'=>'修改失败')));
           }
        }
    }
    
    /**
     * 新建碰撞报告
     */
    public function collision(){
        $pid = session('pid');
        $uid = session('admin_id');
         if(IS_POST){
            $data = I('post.');
            $data['pid'] = $pid;
            $data['uid'] =$uid;
            $data['problem'] = json_encode($data['problem']);
            $data['proposal'] = json_encode($data['proposal']);
            //unset($data['original_edit']);
            $data['status']=5;
            $data['update_time'] = time();
            $data['imgtype'] = $data['picsel'];
            if($data['id']){
                $files = $_FILES;
                if($files['file1']['name'] != '' && $files['file2']['name'] != '' || $files['file3']['name'] != ''){
                    if($data['picsel']==1){
                        unset($_FILES['file3']);
                    }
                    if($data['picsel']==2){
                        unset($_FILES['file1']);
                        unset($_FILES['file2']);
                    }
                    $img = $this->oimg($_FILES);
                    if($data['picsel']==1){
                        $data['original'] = $img[0];
                        $data['original_edit'] = $img[1];
                        unset($data['oimg']);
                    }
                    if($data['picsel']==2){
                        unset($data['original']);
                        unset($data['original_edit']);
                        $data['oimg'] = $img[2];
                    }
                    unset($data['picsel']);
                }else{
                    unset($data['imgtype']);
                }
                unset($data['picsel']);
                $res = M('organization')->where('id='.$data['id'])->save($data);
                if($res){
                    //自动发送任务给设计院或甲方
                    $this->ftask($data['pid'],$data['id']);
                    adminLog("编制了".$id."报告",__ACTION__);
                    $this->success('提交成功',U('Bim/collisionList'));exit;
                }else{
                    $this->error('提交失败');
                }
            }else{
                $file = $_FILES;
                //上传图片
                if($file){
                     if($data['picsel']==1){
                         unset($_FILES['file3']);
                     }
                     if($data['picsel']==2){
                         unset($_FILES['file1']);
                         unset($_FILES['file2']);
                     }
                     $img = $this->oimg($_FILES);
                     if($data['cost']==''){
                         unset($data['cost']);
                     }
                     if($data['time_limit']==''){
                         unset($data['time_limit']);
                     }
                     if($data['picsel']==1){
                         $data['original'] = $img[0];
                         $data['original_edit'] = $img[1];
                         unset($data['oimg']);
                     }
                     if($data['picsel']==2){
                         unset($data['original']);
                         unset($data['original_edit']);
                         $data['oimg'] = $img[2];
                     }
                    unset($data['picsel']);
                    $res = M('organization')->add($data);
                    if($res){
                        $this->ftask($data['pid'],$res);
                        adminLog("新增了".$res."报告",__ACTION__);
                        $this->success('提交成功',U('Bim/collisionList'));exit;
                    }else{
                        $this->error('提交失败');
                    }
                }else{
                    $this->error('请上传图片');
                }
            }
        }
        
        $da = I('get.');
        $design = M('process')->field('copy')->where(array('pid'=>$pid,'type'=>1))->find();
        if(empty($design['copy'])){
            $this->error('抄送人不能为空,请到流程定义中设置抄送人');
        }
        $wint = M('process_data')->field('examine_id')->where(array('pid'=>$pid,'typeid'=>1,'eid'=>2))->find();
        if(empty($wint['examine_id'])){
            $this->error('内审人不能为空,流程定义中未设置内审人员');
        }
        $nail = M('process_data')->field('examine_id')->where(array('pid'=>$pid,'typeid'=>1,'eid'=>3))->find();
        if(empty($nail['examine_id'])){
            $this->error('审核人(甲方)不能为空,流程定义中未设置审核人');
        }
        if($da['status'] == 2){
            $Modal = M('opinion');
            $reject = $Modal->alias('r')
            ->field('r.design as yj,r.add_time,a.name')
            ->join('admin as a ON a.admin_id = r.uid')
            ->where(array('r.oid'=>$da['id'],'r.status'=>3))
            ->order('r.id desc')
            ->select();
            $o = M('organization')->where('id='.$da['id'])->find();
            $this->assign('o',$o);
            $this->assign('reject',$reject);
        }
        if($da['status'] == 1 || $da['status'] == 7){
            $o = M('organization')->where('id='.$da['id'])->find();
            $this->assign('o',$o);
        }
        if($da['status'] == 6){
            $Modal = M('opinion');
            $reject = $Modal->alias('r')
            ->field('r.design as yj,r.add_time,a.name')
            ->join('admin as a ON a.admin_id = r.uid')
            ->where(array('r.oid'=>$da['id'],'r.status'=>4))
            ->order('r.id desc')
            ->select();
            $o = M('organization')->where('id='.$da['id'])->find();
            $this->assign('o',$o);
            $this->assign('reject',$reject);
        }
        
        //查询主题
        $theme = M('theme')->select();
        $status = C('ostatus');//获取状态
        $this->assign('status',$status);
        $this->assign('theme',$theme);
        $this->assign('da',$da);
        $this->assign('nail',$nail['examine_id']);
        $this->assign('design',$design['copy']);
        $this->display();
    }
    
    
    /**
     * 发送任务和站内信
     */
    
    public function ftask($pid,$id){
       // $tid = M('project')->field('total_ren')->where(array('id'=>$pid))->find();
        $tid = M('process_data')->field('examine_id')->where(array('pid'=>$pid,'typeid'=>1,'eid'=>2))->find();
     /*    $a['pid'] = $pid;
        $a['type'] = 3;
        $a['priority'] = 1;
        $a['severity'] = 0;
        $a['t_stop'] = date("Y-m-d H:i:s",strtotime("+1 day"));
        $a['add_time'] = time();
        $a['status'] = 8;
        $a['assigname'] = '自己(我)';
        $a['cfrom'] = session('admin')['name'];
        $a['link'] = $id .','. within;
        $a['assign'] = $tid['examine_id'];
        $a['describe'] = $id.'号编制报告项目管理员内审';
        $a['name'] = $id.'号编制报告内审';
        $d = task($a);
        taskLog("下发了编制".$id."号报告任务",$d); */
        $pname = session('pname');
        $s[] = $tid['examine_id'];
        SendMessage($s,$id.'号编制报告内审','你有内审任务,请到'.$pname.'项目BIM管理-报告文件--碰撞报告中查看');
    }
    
    /**
     * 上传编制图片
     */
    public function oimg($file){
          $setting = C('UPLOAD_PUBLIC_QINIU');
          $Upload = new \Think\Upload($setting);
          $info = $Upload->upload($file);
          if(!$info){
              $this->error('上传图片失败请重试');
          }
          $url1 = $info['file1']['url'];//七牛云保存的文件名称
          $url2 = $info['file2']['url'];//七牛云保存的文件名称
          $url3 = $info['file3']['url'];//七牛云保存的文件名称
          //$Qiniu = new QiniuStorage($setting['driverConfig']);
          if($url2 || $url1){
             // $url1 = $Qiniu->privateDownloadUrl($url1);
             // $url2 = $Qiniu->privateDownloadUrl($url2);
              $a[0] = $url1;
              $a[1] = $url2;
          }
          if($url3){
             // $url3 = $Qiniu->privateDownloadUrl($url3);
              $a[2] = $url3;
          }
          return $a;
    }
    
    
    /**
     * 碰撞报告列表
     */
    public function collisionList(){
        $Modal = M('organization');
        $pid = session('pid');
        $uid = session('admin_id');
        if(!$pid){
            $pid = 0;
        }
        $where['pid'] = array('eq',$pid);
        $ppdata = M('process')->field('launch')->where(array('pid'=>$pid,'type'=>1))->find();
        if(empty($ppdata['launch'])){
            $pdata = 1;
            $this->assign('pdata',$pdata);
        }else{
            $pdata = explode(',',$ppdata['launch']);
            if(in_array($uid,$pdata)){
                $pdata = 2;
                $this->assign('pdata',$pdata);
            }
        }
        $organ = M('organ')->where('pid='.$pid)->find();
        $theme = M('theme')->select();
       
        $this->assign('organ',$organ);
        $this->assign('theme',$theme);
        $this->display();
    }
    
    /**
     * 待我审核碰撞报告列表
     */
    public function ajaxcolist(){
        $Modal = M('organization');
        $pid = session('pid');
        $uid = session('admin_id');
        $themeid = I('post.themeid');
        if(!$pid){
            $pid = 0;
        }
        if($themeid){
            $where['themeid'] = array('eq',$themeid);
        }
        $where['pid'] = array('eq',$pid);
        $res = $Modal->field('id,status,update_time,ban,problem,major,uid,nail,design,themeid')->where($where)->select();
        
        $organ = M('organ')->where('pid='.$pid)->find();//查找编制说明
        $admin = M('admin')->getField('admin_id,name');
        $theme = M('theme')->getField('id,name');//查找主题
        //查询是否有编制说明权限
        if($organ){
            $ppdata = M('process')->field('launch')->where(array('pid'=>$pid,'type'=>1))->find();
            if(!empty($ppdata['launch'])){
                $pdata = explode(',',$ppdata['launch']);
                if(in_array($uid,$pdata)){
                    $pdata = 2;
                    $this->assign('pdata',$pdata);
                }
            }
        }
       // $role = session('role');
        $status = C('ostatus');
        $major = C('major');
        $this->assign('status',$status);
        $this->assign('data',$res);
        $this->assign('organ',$organ);
        $this->assign('major',$major);
        $this->assign('theme',$theme);
        $this->assign('uid',$uid);
        $this->assign('admin',$admin);
        $this->display();
    }
    
    /**
     * ajaxcolisiont待我审核
     */
    public function ajaxcolistt(){
        $Modal = M('organization');
        $pid = session('pid');
        $uid = session('admin_id');
        if(!$pid){
            $pid = 0;
        }
        $where['pid'] = array('eq',$pid);
        //查询内审人ID
        $w = M('process_data')->field('examine_id')->where(array('pid'=>$pid,'typeid'=>1,'eid'=>2))->find();
        if($w['examine_id']==''){
            $w['examine_id']= 0;
        }
        
        $where['status'] = array(array('eq',3),array('eq',5),array('eq',4),'or') ;
       // $where['_query'] = 'design='.$uid.'&nail='.$uid.'&'.$w['examine_id'].'='.$uid.'&_logic=or';
        $res = $Modal->field('id,status,update_time,ban,problem,major,uid,design,nail,themeid')->where($where)->select();
        //判断是否有抄送
        foreach ($res as $key=>$val){
            if($val['status'] == 3 || $val['status'] ==4){
                $pcopy = explode(',',$val['design']);
                if(in_array($uid,$pcopy)){
                   $res[$key]['pcopy'] = 2;
                }
                $id[] = $val['id'];
            }
        }
        if(!empty($id)){
            //查询抄送意见是否存在
            $id = implode(',',$id);
            $map['status'] = array('eq',1);
            $map['oid'] = array('in',$id);
            $map['uid'] = array('eq',$uid);
            $opinion = M('opinion')->field('oid,uid')->where($map)->select();
        }
        //判断是否提交了意见
        if($opinion !='' && $res!=''){
            foreach ($opinion as $o=>$p){
                foreach ($res as $r=>$e){
                    if($p['oid'] == $e['id'] && $p['uid']==$uid){
                        $res[$r]['copy'] = 2;
                    }
                }
            }
        }
        //查询内审
        if($w['examine_id']==$uid){
            $within = 2;
            $this->assign('within',$within);
        }
        
        $major = C('major');
        $admin = M('admin')->getField('admin_id,name');
        $theme = M('theme')->getField('id,name');//查找主题
        $status = C('ostatus');
     
        $this->assign('uid',$uid);
        $this->assign('status',$status);//状态
        $this->assign('admin',$admin);//编制人
        $this->assign('major',$major);//专业
        $this->assign('theme',$theme);
        $this->assign('data',$res);
        $this->display();
    }
    
    
    
    /**
     * 碰撞报告详情页
     */
    public function collisionDetail(){
        $id = I('GET.id');
        $Modal = M('admin');
        $opinion = M('opinion');
        if($id){
            $data = M('organization')->where('id='.$id)->find();
            if($data['status'] ==4){
                $data['opinion'] = $opinion->field('design')->where(array('oid'=>$id,'status'=>2))->find();
                $data['odesign'] = $opinion->alias('o')
                                           ->field('o.design,a.name')
                                           ->join('admin as a ON a.admin_id = o.uid')
                                           ->where(array('o.oid'=>$id,'o.status'=>1))->select();
            }
            if($data['status'] == 2){
                $Moda = M('opinion');
                $reject = $Moda->alias('r')
                ->field('r.design as yj,r.add_time,a.name')
                ->join('admin as a ON a.admin_id = r.uid')
                ->where(array('r.oid'=>$id,'r.status'=>3))
                ->order('r.id desc')
                ->select();
                $this->assign('reject',$reject);
            }
            
            if($data['status'] == 6){
                $Moda = M('opinion');
                $reject = $Moda->alias('r')
                ->field('r.design as yj,r.add_time,a.name')
                ->join('admin as a ON a.admin_id = r.uid')
                ->where(array('r.oid'=>$id,'r.status'=>4))
                ->order('r.id desc')
                ->select();
                $this->assign('reject',$reject);
            }
            
            $data['uname'] = $Modal->field('name')->where('admin_id='.$data['uid'])->find();
            $pname = M('project')->field('total_ren,name')->where('id='.$data['pid'])->find();
            $data['pname'] = $pname['name'];
            $data['tname'] = $Modal->field('name')->where('admin_id='.$pname['total_ren'])->find();
            $data['nname'] = $Modal->field('name')->where('admin_id='.$data['nail'])->find();
            $map['admin_id'] = array('in',$data['design']);
            $data['dname'] = $Modal->field('name')->where($map)->select();
            foreach ($data['dname'] as $k=>$v){
                $c[] = $v['name'];
            }
            $b = implode(',',$c);
            $data['dname'] = $b;
            $status = C('ostatus');
            $major = C('major');//获取专业
            $optimization = C('optimization');
            $this->assign('optimization',$optimization);
            $this->assign('status',$status);
            $this->assign('major',$major);
            $this->assign('data',$data);
        }else{
            $this->error('id有误,请重试');
        }
        $this->display();
    }
    
    /**
     * 碰撞报告详情打印
     */
    public function collisionprint(){
        $id = I('GET.id');
        $Modal = M('admin');
        $opinion = M('opinion');
        if($id){
            $data = M('organization')->where('id='.$id)->find();
            if($data['status'] ==4){
                $data['opinion'] = $opinion->field('design')->where(array('oid'=>$id,'status'=>2))->find();
                $data['odesign'] = $opinion->alias('o')
                ->field('o.design,a.name')
                ->join('admin as a ON a.admin_id = o.uid')
                ->where(array('o.oid'=>$id,'o.status'=>1))->select();
            }
            $data['uname'] = $Modal->field('name')->where('admin_id='.$data['uid'])->find();
            $pname = M('project')->field('total_ren,name')->where('id='.$data['pid'])->find();
            $data['pname'] = $pname['name'];
            $data['tname'] = $Modal->field('name')->where('admin_id='.$pname['total_ren'])->find();
            $data['nname'] = $Modal->field('name')->where('admin_id='.$data['nail'])->find();
            $map['admin_id'] = array('in',$data['design']);
            $data['dname'] = $Modal->field('name')->where($map)->select();
            foreach ($data['dname'] as $k=>$v){
                $c[] = $v['name'];
            }
            $b = implode(',',$c);
            $data['dname'] = $b;
            $status = C('ostatus');
            $major = C('major');//获取专业
            $optimization = C('optimization');
            $this->assign('optimization',$optimization);
            $this->assign('status',$status);
            $this->assign('major',$major);
            $this->assign('data',$data);
        }else{
            $this->error('id有误,请重试');
        }
        $this->display();
    }
    
    /**
     * 甲方
     */
    public function nail(){
        $id = I('get.id');
        $Modal = M('opinion');
        $data = M('organization')->where('id='.$id)->find();
        if(!$data){
            $this->error('编制报告不存在或已被删除');
        }
        if($data['status'] == 2){
            $a = $Modal->field('design')->where(array('oid'=>$id,'uid'=>session('admin_id'),'status'=>3))->find();
            $this->assign('a',$a);
        }
        if($data['status'] == 4){
            $b = $Modal->field('design')->where(array('oid'=>$id,'uid'=>session('admin_id'),'status'=>2))->find();
            $this->assign('b',$b);
        }
        
        $data['pname'] = M('project')->field('name')->where('id='.$data['pid'])->find();
        $data['uname'] = M('admin')->field('name')->where('admin_id='.$data['uid'])->find();
        $status = C('ostatus');//获取状态
        $major = C('major');//获取专业
        $optimization = C('optimization');//获取优化点
        $this->assign('optimization',$optimization);
        $this->assign('status',$status);
        $this->assign('major',$major);
        $this->assign('data',$data);
        $this->display();
    }
    
    /**
     * 甲方提交意见
     */
    public function t_nail(){
        $id = I('post.id');
        $design = json_encode(I('post.design'));
        $uid = session('admin_id');
        $Modal = M('opinion');
        $res = $Modal->where(array('oid'=>$id,'uid'=>$uid,'status'=>2))->count();
        if($res){
            $re = $Modal->where(array('oid'=>$id,'uid'=>$uid,'status'=>2))->save(array('design'=>$design));
        }else{
            $ad['oid'] = $id;
            $ad['uid'] = $uid;
            $ad['status'] = 2;
            $ad['design'] = $design;
            $ad['add_time'] = time();
            $re = $Modal->add($ad);
        }
        if($re){
            M('organization')->where('id='.$id)->save(array('status'=>4,'time_stop'=>time()));
            adminLog("甲方通过了".$id."报告并提交了意见",__ACTION__);
            exit(json_encode(array('errNo'=>0,'msg'=>'已通过')));
        }else{
            exit(json_encode(array('errNo'=>1,'msg'=>'提交失败,请重试')));
        }
    }
    
    /**
     * 甲方驳回意见
     */
    public function reject(){
        $id = I('post.id');
        $uuid = I('post.uid');
        $pname = session('pname');
        $design = json_encode(I('post.design'));
        $uid = session('admin_id');
        $Modal = M('opinion');
        $res = $Modal->where(array('oid'=>$id,'uid'=>$uid,'status'=>3))->count();
        if($res){
            $re = $Modal->where(array('oid'=>$id,'uid'=>$uid,'status'=>3))->save(array('design'=>$design));
        }else{
            $ad['oid'] = $id;
            $ad['uid'] = $uid;
            $ad['status'] = 3;
            $ad['design'] = $design;
            $ad['add_time'] = time();
            $re = $Modal->add($ad);
        }
        if($re){
            $c[] = $uuid;
            SendMessage($c,'编制报告被驳回','你的'.$id.'.号编制报告被驳回请到'.$pname.'项目编制报告中查看');
            M('organization')->where('id='.$id)->save(array('status'=>2,'time_stop'=>time()));
            adminLog("甲方驳回了".$id."报告",__ACTION__);
            exit(json_encode(array('errNo'=>0,'msg'=>'驳回成功')));
        }else{
            exit(json_encode(array('errNo'=>1,'msg'=>'驳回失败,请重试')));
        }
    }
    
    /**
     * 内审
     */
    public function within(){
        $id = I('get.id');
      //  $tid = I('get.tid');
        $Modal = M('opinion');
        $data = M('organization')->where('id='.$id)->find();
        if(!$data){
            $this->error('编制报告不存在或已被删除');
        }
        if($data['status']==6){
            $a = $Modal->field('design')->where(array('oid'=>$id,'uid'=>session('admin_id'),'status'=>4))->find();
            $this->assign('a',$a);
        }
        $data['pname'] = M('project')->field('name')->where('id='.$data['pid'])->find();
        $data['uname'] = M('admin')->field('name')->where('admin_id='.$data['uid'])->find();
        $status = C('ostatus');//获取状态
        $major = C('major');//获取专业
        $optimization = C('optimization');//获取优化点
        $this->assign('optimization',$optimization);
        $this->assign('status',$status);
        $this->assign('major',$major);
        $this->assign('data',$data);
       // $this->assign('tid',$tid);
        $this->display();
    }
    
    /**
     * 内审驳回
     */
    public function rwithin(){
        $id = I('post.id');
        $uuid = I('post.uid');
        $pname = session('pname');
        $design = json_encode(I('post.design'));
        $uid = session('admin_id');
        $Modal = M('opinion');
        $res = $Modal->where(array('oid'=>$id,'uid'=>$uid,'status'=>4))->count();
        if($res){
            $re = $Modal->where(array('oid'=>$id,'uid'=>$uid,'status'=>4))->save(array('design'=>$design));
        }else{
            $ad['oid'] = $id;
            $ad['uid'] = $uid;
            $ad['status'] = 4;
            $ad['design'] = $design;
            $ad['add_time'] = time();
            $re = $Modal->add($ad);
        }
        if($re){
            $c[] = $uuid;
            SendMessage($c,$id.'号编制报告被驳回',$id.'号编制报告被驳回请到'.$pname.'项目编制报告列表中查看');
            M('organization')->where('id='.$id)->save(array('status'=>6,'time_stop'=>time()));
            adminLog("内审驳回了".$id."报告",__ACTION__);
            exit(json_encode(array('errNo'=>0,'msg'=>'驳回成功')));
        }else{
            exit(json_encode(array('errNo'=>1,'msg'=>'驳回失败,请重试')));
        }
    }
    
    /**
     * 内审通过
     */
    public function twithin(){
        $id = I('post.id');
        $pname = session('pname');
        $Modal = M('organization');
            $data = $Modal->where('id='.$id)->find();
            if($data){
                $a = $Modal->where('id='.$id)->save(array('status'=>3,'time_stop'=>time()));
                if($a){
                    if($data['design']){
                        $design = explode(',',$data['design']);
                        foreach($design as $key){
                            $s[] = $key;
                        }
                        SendMessage($s,$id.'号编制报告抄送',$id.'号抄送编制报告,请到 '.$pname.'项目 BIM管理-报告文件--碰撞报告中查看');
                    }
                    if($data['nail']){
                        $c[] = $data['nail'];
                        SendMessage($c,$id.'号编制报告审核',$id.'号审核任务,请到'.$pname.'项目  BIM管理-报告文件--碰撞报告中查看');
                    }
                    adminLog("内审通过了".$id."任务",__ACTION__);
                    exit(json_encode(array('errNo'=>0,'msg'=>'已通过')));
                }
            }else{
                exit(json_encode(array('errNo'=>1,'msg'=>'通过失败,请重试')));
            }
    }
    
    /**
     * 设计院
     */
    public function design(){
        $id = I('get.id');
        $data = M('organization')->where('id='.$id)->find();
        if(!$data){
            $this->error('编制报告不存在或已被删除');
        }
        $data['pname'] = M('project')->field('name')->where('id='.$data['pid'])->find();
        $data['uname'] = M('admin')->field('name')->where('admin_id='.$data['uid'])->find();
        $re = M('opinion')->field('design')->where(array('oid'=>$id,'uid'=>session('admin_id'),'status'=>1))->find();
        $status = C('ostatus');//获取状态
        $major = C('major');//获取专业
        $optimization = C('optimization');//获取优化点
        $this->assign('optimization',$optimization);
        $this->assign('re',$re); 
        $this->assign('status',$status);
        $this->assign('major',$major);
        $this->assign('data',$data);
        $this->display();
    }
    
    /**
     * 设计院意见提交
     */
     public function o_design(){
         $Modal = M('opinion');
         $data = I('post.');
         
         $data['uid'] = session('admin_id');
         $data['design'] = json_encode(I('post.design'));
         $data['status'] = 1;
         $data['add_time'] = time();
         $res = $Modal->where(array('oid'=>$data['oid'],'uid'=>$data['uid'],'status'=>1))->count();
         if($res){
             $re = $Modal->where(array('oid'=>$data['oid'],'uid'=>$data['uid'],'status'=>1))->save(array('design'=>$data['design']));
         }else{
             $re = $Modal->add($data);
         }
         if($re){
               adminLog("设计院对".$data['oid']."号报告提交了意见",__ACTION__);
             exit(json_encode(array('errNo'=>0,'msg'=>'提交意见成功')));
         }else{
             exit(json_encode(array('errNo'=>1,'msg'=>'提交失败')));
         }
    } 
    
    /**
     * 删除编制报告
     */
    public function odel(){
        $id = I('post.id');
        if(!$id){
            exit(json_encode(array('errNo'=>1,'msg'=>'id不能为空')));
        }else{
           $r =  M('organization')->where(array('id'=>$id))->delete();
        }
           if($r){
               adminLog('删除编制报告',__ACTION__);
               exit(json_encode(array('errNo'=>0,'msg'=>'删除成功')));
           }else{
               exit(json_encode(array('errNo'=>1,'msg'=>'删除失败')));
           }
    }
    
    
    /**
     * 导入页面数据
     */
    public function imdata(){
        $data = json_decode($_POST['data'], true);
        if(empty($data)){
            exit(json_encode(array('errNo'=>1,'msg'=>'数据不能为空')));
        }
        $uid = session('admin_id');
        $pid = session('pid');
        $time = time();
        if($data[0]==''){
            unset($data[0]);
        }
        if($data[1]==''){
            unset($data[1]);
        }
        foreach ($data as $val){
             $o = explode(':',$val[0]);
             $axis = $o[0];
             $axis_turn = $o[1];
             $dataList[] = array('uid'=>$uid,'pid'=>$pid,'update_time'=>$time,'floor'=>$val[1],'position'=>$val[2],'position_turn'=>$val[3],'axis'=>$axis,'axis_turn'=>$axis_turn,'problem'=>'暂无','proposal'=>'暂无');
        }
        $Modal = D('organization');
        $re = $Modal->addAll($dataList);
        if($re){
            adminLog('导入编制报告',__ACTION__);
            exit(json_encode(array('errNo'=>0,'msg'=>'导入成功')));
        }else{
            exit(json_encode(array('errNo'=>1,'msg'=>'导入失败')));
        }
    }
    
    /**
     * 设置形象照片
     */
    public function simg(){
        $name = I('post.name');
        if(empty($name)){
            exit(json_encode(array('errNo'=>1,'msg'=>'数据有误')));
        }
        $pid = session('pid');
        if(pid){
            $a = M('project')->field('image')->where('id='.$pid)->find();
            if($a['image']==$name){
                exit(json_encode(array('errNo'=>1,'msg'=>'已为当前图片,无需重复设置')));
            }else{
                $b = M('project')->where('id='.$pid)->save(array('image'=>$name));
                if($b){
                    exit(json_encode(array('errNo'=>0,'msg'=>'设置成功')));
                }else{
                    exit(json_encode(array('errNo'=>1,'msg'=>'设置失败')));
                }
            }
        }
    }
    
    
    /**实现导入excel
     **/
    function impUser(){
        if (!empty($_FILES)) {
            //import("@.ORG.UploadFile");
            $config=array(
                'exts'=>array('xlsx','xls'),
                'rootPath'=>"./Uploads/excel/",
                'savePath'=>'',
                //'autoSub'    =>    true,
                'subName'    =>    array('date','Ymd'),
            );
            $upload = new \Think\Upload($config);
            //var_dump($upload);exit;
            if (!$info=$upload->upload()) {
                $this->error($upload->getError());
            } /*else {
            //$info = $upload->getUploadFileInfo();
    
            }
            */
            //var_dump($_FILES);exit;
            vendor("PHPExcel.PHPExcel");
            $file_name=$upload->rootPath.$info['import']['savepath'].$info['import']['savename'];
            //var_dump($file_name);exit;
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow(); // 取得总行数
            $highestColumn = $sheet->getHighestColumn(); // 取得总列数
            for($i=3;$i<=$highestRow;$i++)
            {
                $data['account']= $data['truename'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
                $sex = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
                // $data['res_id']    = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
                $data['class'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
                $data['year'] = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
                $data['city']= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
                $data['company']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
                $data['zhicheng']= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
                $data['zhiwu']= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
                $data['jibie']= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
                $data['honor']= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
                $data['tel']= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
                $data['qq']= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
                $data['email']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
                $data['remark']= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
                $data['sex']=$sex=='男'?1:0;
                $data['res_id'] =1;
    
                $data['last_login_time']=0;
                $data['create_time']=$data['last_login_ip']=$_SERVER['REMOTE_ADDR'];
                $data['login_count']=0;
                $data['join']=0;
                $data['avatar']='';
                $data['password']=md5('123456');
                M('Member')->add($data);
                	
            }
            $this->success('导入成功！');
        }else
        {
            $this->error("请选择上传的文件");
        }
    }
    
    /**
     * 初始化编辑器链接
     * 本编辑器参考 地址 http://fex.baidu.com/ueditor/
     */
    private function initEditor()
    {
        $this->assign("URL_upload", U('Home/Ueditor/upimage',array('savepath'=>'img'))); // 图片上传目录
        $this->assign("URL_imageUp", U('Home/Ueditor/imageUp',array('savepath'=>'img'))); //  不知道啥图片
        $this->assign("URL_fileUp", U('Home/Ueditor/fileUp',array('savepath'=>'img'))); // 文件上传s
        $this->assign("URL_scrawlUp", U('Home/Ueditor/scrawlUp',array('savepath'=>'img')));  //  图片流
        $this->assign("URL_getRemoteImage", U('Home/Ueditor/getRemoteImage',array('savepath'=>'img'))); // 远程图片管理
        $this->assign("URL_imageManager", U('Home/Ueditor/imageManager',array('savepath'=>'img'))); // 图片管理
        $this->assign("URL_getMovie", U('Home/Ueditor/getMovie',array('savepath'=>'img'))); // 视频上传
        $this->assign("URL_Home", "");
    }
    
    /**
     * 编制说明
     */
    public function explain(){
        $act = I('get.');
        if($act['act'] == 'edit'){
           $organ = M('organ')->where('id='.$act['id'])->find();
            $this->assign('organ',$organ);
        }
        $this->assign('act',$act['act']);
        $this->initEditor();
        $this->display();
    }
    
    /**
     * 编制说明添加和编辑
     */
    public function explainae(){
        $data = I('post.');
        $pid = session('pid');
        if(!$pid){
            $this->error('项目ID有误');
        }
        if($data == ''){
            $this->error('数据不能为空');
        }
        if($data['act'] == 'edit'){
            $r = M('organ')->where('pid='.$pid)->save(array('content'=>$data['content']));
            adminLog('编辑编制说明',__ACTION__);
        }else{
            $cont =  M('organ')->where('pid='.$pid)->count();
            if($cont){
                $this->error('编制说明已存在');
            }
            $da['add_time'] = time();
            $da['content'] = $data['content'];
            $da['pid'] = $pid;
            $r = M('organ')->add($da);
            adminLog('添加编制说明',__ACTION__);
        }
        if($r){
            $this->success("操作成功",U('Bim/collisionList'));
            exit;
        }else{
            $this->error("操作失败");
        }
    }
    
    
    /**
     * 编制说明详情页
     */
    public function organdetail(){
        $id = I('GET.id');
        $pid = session('pid');
        if(!$id){
            $this->error('id有误');
        }
        $orga = M('organ')->where('id='.$id)->find();
        
        //优化点和工程成本统计
        $op['pid'] = array('eq',$pid);
        $op['optimization'] = array('neq',0);
        $organ = M('organization')->field('optimization,status,time_limit,cost,id')->where($op)->select();
        $design =0;
        $optimization=0;
        $check=0;
        $construction=0;
        $time=0;
        $costt=0;
        foreach ($organ as $k=>$v){
            if($v['status'] != 2 && $v['status'] != 6){
                 if($v['cost'] != 0){
                    $cost[] = $v['cost'];
                }
                if(!empty($v['id']) && $v['cost'] !=0 || $v['time_limit'] !=0){
                    $bid[] = $v['id'].'号报告';
                }
                if($v['time_limit'] != 0){
                    $time_limit[] = $v['time_limit'];
                }
                if($v['optimization'] == 1){
                    $design++;
                }
                if($v['optimization'] == 2){
                    $optimization++;
                }
                if($v['optimization'] == 3){
                    $check++;
                }
                if($v['optimization'] == 4){
                    $construction++;
                }
                if($v['optimization'] == 5){
                    $time++;
                }
                if($v['optimization'] == 6){
                    $costt++;
                }
            }
        }
        if($cost == null && $time_limit == null){
            $gc = 1;
            $this->assign('gc',$gc);
        }
        $cost = json_encode($cost);
        $time_limit = json_encode($time_limit);
        $bid = json_encode($bid);
        $ordata['design'] = $design;
        $ordata['optimization'] = $optimization;
        $ordata['check'] = $check;
        $ordata['construction'] = $construction;
        $ordata['time'] = $time;
        $ordata['cost'] = $costt;
        $ordata = json_encode($ordata);
        $organs = json_encode(array($design,$optimization,$check,$construction,$time,$costt));
        
        $this->assign('ordata',$ordata);
        $this->assign('organs',$organs);
         $this->assign('bid',$bid);
        $this->assign('cost',$cost);
        $this->assign('time_limit',$time_limit);
        $this->assign('organ',$orga);
        $this->display();
    }
    
    /**
     * 编制说明详情页打印
     */
    public function organdetailprint(){
        $id = I('GET.id');
        $pid = session('pid');
        if(!$id){
            $this->error('id有误');
        }
        $orga = M('organ')->where('id='.$id)->find();
    
        //优化点和工程成本统计
        $op['pid'] = array('eq',$pid);
        $op['optimization'] = array('neq',0);
        $organ = M('organization')->field('optimization,status,time_limit,cost,id')->where($op)->select();
        $design =0;
        $optimization=0;
        $check=0;
        $construction=0;
        $time=0;
        $costt=0;
        foreach ($organ as $k=>$v){
            if($v['status'] != 2 && $v['status'] != 6){
                if($v['cost'] != 0){
                    $cost[] = $v['cost'];
                }
                if(!empty($v['id']) && $v['cost'] !=0 || $v['time_limit'] !=0){
                    $bid[] = $v['id'].'号报告';
                }
                if($v['time_limit'] != 0){
                    $time_limit[] = $v['time_limit'];
                }
                if($v['optimization'] == 1){
                    $design++;
                }
                if($v['optimization'] == 2){
                    $optimization++;
                }
                if($v['optimization'] == 3){
                    $check++;
                }
                if($v['optimization'] == 4){
                    $construction++;
                }
                if($v['optimization'] == 5){
                    $time++;
                }
                if($v['optimization'] == 6){
                    $costt++;
                }
            }
        }
        if($cost == null && $time_limit == null){
            $gc = 1;
            $this->assign('gc',$gc);
        }
        $cost = json_encode($cost);
        $time_limit = json_encode($time_limit);
        $bid = json_encode($bid);
        $ordata['design'] = $design;
        $ordata['optimization'] = $optimization;
        $ordata['check'] = $check;
        $ordata['construction'] = $construction;
        $ordata['time'] = $time;
        $ordata['cost'] = $costt;
        $ordata = json_encode($ordata);
        $organs = json_encode(array($design,$optimization,$check,$construction,$time,$costt));
    
        $this->assign('ordata',$ordata);
        $this->assign('organs',$organs);
        $this->assign('bid',$bid);
        $this->assign('cost',$cost);
        $this->assign('time_limit',$time_limit);
        $this->assign('organ',$orga);
        $this->display();
    }
    
    /**
     * 编辑编制报告
     */
    public function editcollision(){
        $Modal = M('organization');
        if(IS_POST){
            $data = I('post.');
            $data['problem'] = json_encode($data['problem']);
            $data['proposal'] = json_encode($data['proposal']);
            $data['update_time'] = time();
            $data['imgtype'] = $data['picsel'];
            if($data['id']){
               /*  $data['problem'] = json_encode($data['problem']);
                $data['proposal'] = json_encode($data['proposal']);
                $data['update_time'] = time();
                $data['imgtype'] = $data['picsel']; */
                    $files = $_FILES;
                    if($files['file1']['name'] != '' && $files['file2']['name'] != '' || $files['file3']['name'] != ''){
                        if($data['picsel']==1){
                            unset($_FILES['file3']);
                        }
                        if($data['picsel']==2){
                            unset($_FILES['file1']);
                            unset($_FILES['file2']);
                        }
                        $img = $this->oimg($_FILES);
                        if($data['picsel']==1){
                            $data['original'] = $img[0];
                            $data['original_edit'] = $img[1];
                            unset($data['oimg']);
                        }
                        if($data['picsel']==2){
                            unset($data['original']);
                            unset($data['original_edit']);
                            $data['oimg'] = $img[2];
                        }
                        unset($data['picsel']);
                    }else{
                        unset($data['imgtype']);
                    }
                    unset($data['picsel']);
                    $res = $Modal->where('id='.$data['id'])->save($data);
                    if($res){
                        $this->success('编辑成功',U('Bim/collisionList'));exit;
                    }else{
                        $this->error('编辑失败');
                    }
            }else{
                $pid = session('pid');
                $uid = session('admin_id');
                $data['pid'] = $pid;
                $data['uid'] =$uid;
                $data['status']=7;
                $file = $_FILES;
                //上传图片
                if($file){
                    if($data['picsel']==1){
                        unset($_FILES['file3']);
                    }
                    if($data['picsel']==2){
                        unset($_FILES['file1']);
                        unset($_FILES['file2']);
                    }
                    $img = $this->oimg($_FILES);
                    if($data['cost']==''){
                        unset($data['cost']);
                    }
                    if($data['time_limit']==''){
                        unset($data['time_limit']);
                    }
                    if($data['picsel']==1){
                        $data['original'] = $img[0];
                        $data['original_edit'] = $img[1];
                        unset($data['oimg']);
                    }
                    if($data['picsel']==2){
                        unset($data['original']);
                        unset($data['original_edit']);
                        $data['oimg'] = $img[2];
                    }
                    unset($data['picsel']);
                    $res = M('organization')->add($data);
                    if($res){
                        adminLog("把".$res."报告保存为了草稿",__ACTION__);
                        $this->success('保存草稿成功',U('Bim/collisionList'));exit;
                    }else{
                        $this->error('保存草稿失败');
                    }
                }else{
                    $this->error('请上传图片');
                }
            }
        }
        
        $id = I('get.id');
        if(!$id){
            $this->error('编辑ID有误，请重试');
        }
        $o = $Modal->where('id='.$id)->find();
        $this->assign('o',$o);
        $this->display();
    }
    
    /**
     * 新增主题
     */
    function add_theme(){
        $name = I('post.name');
        if(empty($name)){
            exit(json_encode(array('errNo' => 1, 'msg' => '主题名称不能为空')));
        }
        $Modal = M('theme');
        $t = $Modal->where(array('name'=>$name))->count();
        if($t){
            exit(json_encode(array('errNo' => 1, 'msg' => '主题已存在')));
        }else{
            $data['add_time'] = time();
            $data['name'] = $name;
            $re = $Modal->add($data);
            if($re){
                $res['id'] = $re;
                $res['name'] = $name;
                exit(json_encode(array('errNo' => 0, 'msg' => '新增成功','data'=>$res)));
            }else{
                exit(json_encode(array('errNo' => 1, 'msg' => '新增失败')));
            }
        }
    }
}